﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace homework10
{
    /// <summary>
    /// WinLogs.xaml 的交互逻辑
    /// </summary>
    public partial class WinLogs : Window
    {
        public WinLogs()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dtpFrom.SelectedDateFormat = DatePickerFormat.Short;
            dtpFrom.SelectedDate = System.DateTime.Now;
            dtpTo.SelectedDateFormat = DatePickerFormat.Short;
            dtpTo.SelectedDate = System.DateTime.Now;
            ShowLogsInListView();
        }

        public void ShowLogsInListView()
        {
            //lvwSchool.Items.Clear();
            using (var context = new AppDbContext())
            {
                context.Logs.Load();
                lvwLogs.ItemsSource = context.Logs.Local.ToObservableCollection();
            }
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (var context = new AppDbContext())
                {
                    var query = context.Logs
                        .Where(l => ((l.Timestamp > dtpFrom.SelectedDate) && (l.Timestamp < dtpTo.SelectedDate)));
                    //query.Load();
                    lvwLogs.ItemsSource = query.ToList();// query;//.Local.ToObservableCollection();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "发生错误",MessageBoxButton.OK );
            }
        }
    }
}
